package com.example.mall.controller;

import com.alibaba.fastjson.JSON;
import com.example.mall.pojo.*;
import com.example.mall.service.SpManagerService;
import com.example.mall.service.UserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

//
@RestController
@AllArgsConstructor
@CrossOrigin
@Slf4j
public class UserController {

    UserService userService;
    SpManagerService spManagerService;


    @PutMapping("/users/{id}/role")
    public Rbody updateUser(@PathVariable("id") String id, @RequestBody String body, HttpServletRequest request){
        Rbody rbody = new Rbody();
        System.out.println("!!!"+body);
        UpdateRole role = JSON.parseObject(body, UpdateRole.class);
//        SpManager mm = spManagerService.getById(role.getRid());
//        Integer mg_id = mm.getMg_id();
        Boolean aBoolean = userService.updateRoles(role.getRid(), Integer.valueOf(id));
        if(!aBoolean){
            rbody.getMeta().setMsg("更改失败");
            rbody.getMeta().setStatus(400);
        }
//        System.out.println("mg_id"+mg_id);
//        userService.updateRoles(role.getRid(),)
        rbody.getMeta().setMsg("更改成功");
        rbody.getMeta().setStatus(200);
        return rbody;
    }

//    查询用户列表并分类
    @GetMapping("/users")
    public Rbody getUserList(@RequestParam("query") String query, @RequestParam("pagenum")Integer pagenum, @RequestParam("pagesize")Integer pagesize){
        Rbody rbody = new Rbody();
        OnePage op = userService.getUserList(query,pagenum,pagesize);
        rbody.setData(op);
        System.out.println(op);
        rbody.getMeta().setMsg("查询用户列表成功");
        rbody.getMeta().setStatus(200);
        return rbody;
    }

//    删除用户
    @DeleteMapping("/users/{id}")
    public Rbody deleteUser(@PathVariable("id") Integer id) {
        Rbody rbody = new Rbody();
        Boolean del = userService.deleteUser(id);
        if (del) {
            rbody.getMeta().setMsg("删除用户成功");
            rbody.getMeta().setStatus(200);
        }
        return rbody;
    }

//    根据id查询用户
    @GetMapping("/users/{id}")
    public Rbody getInfoById(@PathVariable("id") Integer id){
        Rbody rbody = new Rbody();
        System.out.println("++++++++++"+id);
        UserById spManager = userService.getUserInfoById(id);
        rbody.setData(spManager);
        System.out.println("+++"+spManager);
        rbody.getMeta().setMsg("根据id查询用户列表成功");
        rbody.getMeta().setStatus(200);

        return rbody;
    }

//    插入用户
    @PostMapping("/users")
    public Rbody insertUser(@RequestBody String body,UserOut userOut){
        Rbody rbody = new Rbody();
        System.out.println(body);
        Useradd useradd = JSON.parseObject(body, Useradd.class);
        UserOut userOut1 = new UserOut();

        String username = useradd.getUsername();
        String password = useradd.getPassword();
        String email = useradd.getEmail();
        String mobile = useradd.getMobile();

//        userOut1.setUsername(username);
//        userOut1.setPassword(password);
//        userOut1.setEmail(email);
//        userOut1.setMobile(mobile);
        String password1 = DigestUtils.md5DigestAsHex(useradd.getPassword().getBytes());
        System.out.println("password1"+password1);
        userOut1.setUsername(username);
        userOut1.setPassword(password1);
        userOut1.setEmail(email);
        userOut1.setMobile(mobile);
//            userOut1.setCreate_time(String.valueOf(1486720211));
        rbody.setData(userOut1);

        Boolean aBoolean = userService.insertUser(userOut1);

        if(!aBoolean){
            rbody.getMeta().setMsg("添加用户失败");
            rbody.getMeta().setStatus(400);
        }else {
            rbody.getMeta().setMsg("添加用户成功");
            rbody.getMeta().setStatus(201);
        }

        return rbody;
    }

//    根据id修改状态
    @PutMapping("users/{uId}/state/{type}")
    public Rbody UpdateStateById(@PathVariable("uId") Integer uId, @PathVariable("type") Boolean type) {
        Rbody rbody = new Rbody();
        Boolean updateStateById = userService.updateStateById(type,uId);
        if (updateStateById) {

            rbody.getMeta().setMsg("根据id修改用户状态成功！！！");
            rbody.getMeta().setStatus(200);
        }
        return rbody;
    }


//    根据id修改用户信息
    @PutMapping("users/{id}")
    public Rbody UpdateInfo(@RequestBody String body,@PathVariable("id") Integer id) {
        Rbody rbody = new Rbody();
        System.out.println("------"+body);
        UserById userById = JSON.parseObject(body, UserById.class);
        String username = userById.getUsername();
        Boolean updateInfo = userService.updateInfo(userById.getEmail(),userById.getMobile(),id);

        if (updateInfo) {
            rbody.getMeta().setMsg("修改用户邮箱和电话成功！！！");
            rbody.getMeta().setStatus(200);
        }else {
            rbody.getMeta().setMsg("修改用户邮箱和电话失败！！！");
            rbody.getMeta().setStatus(500);
        }
        return rbody;

    }

    //    根据id修改状态
//    @PutMapping("users/{id}/role")
//    public Rbody UpdateRole(@PathVariable("id") Integer id, @RequestBody String body) {
//        Rbody rbody = new Rbody();
//        Boolean updateRole = userService.updateRole(id);
//        RoleList roleList = JSON.parseObject(body, RoleList.class);
//
//        Integer rid =.getRid();
//        userById.setRid(rid);
//        System.out.println(rid);
//
//        if (updateRole) {
//            userById.setRid(rid);
//            rbody.getMeta().setMsg("分配角色成功！！！");
//            rbody.getMeta().setStatus(200);
//        }
//        return rbody;
//    }

////    根据id获取role_name
//    @GetMapping("/roles")
//    public Rbody SelectRole(){
//        Rbody rbody = new Rbody();
//        List<RoleList> roleList = userService.selectRole();
//        System.out.println(roleList);
//        if (roleList==null) {
//            rbody.getMeta().setMsg("获取角色失败");
//            rbody.getMeta().setStatus(400);
//        }else {
//            rbody.setData(roleList);
//            rbody.getMeta().setMsg("获取角色成功");
//            rbody.getMeta().setStatus(200);
//        }
//        return rbody;
//    }
}
